<?php
class wqClassAction extends ActionNot{




    public function initAction()
    {
        $this->mweblogin(0, true);
    }



    public function wqdwAction()
    {

        if ( isajax() ) {

            $imagepath = $_POST["imagepath"];
            $explain = $_POST["explain"];
            $address = $_POST["address"];
            $customer = $_POST["customer"];

            //请求url地址
            $appId = 'wxc2a0cb6fd674969a';
            $appSecret = 'af42fc23fa7be3f61270b7fee216cd45';
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
            //初始化curl
            $ch = curl_init($url);
            //3.设置参数
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            //4.调用接口
            $res = curl_exec($ch);
            if(curl_errno($ch)){
                var_dump(curl_error($ch));
            }
            $resArr = json_decode($res,1);
            //5.关闭curl
            curl_close($ch);

            $access_token = $resArr['access_token'];


            $media_id = $imagepath;


            //根据微信JS接口上传了图片,会返回上面写的images.serverId（即media_id），填在下面即可

            $str = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$media_id";


            //获取微信“获取临时素材”接口返回来的内容（即刚上传的图片）
            $a = file_get_contents($str);

           //__DIR__指向当前执行的PHP脚本所在的目录

            //以读写方式打开一个文件，若没有，则自动创建

            $time = date('Y-m-d H:i:s');
            $time1 = date('Y-m-d');

            if (!is_dir("./upload/data/$time1")) mkdir("./upload/data/$time1");

            $imgname = rand(1,9999);

            $resource = fopen("./upload/data/$time1/$imgname.jpg" , 'w+');
            //将图片内容写入上述新建的文件
            fwrite($resource, $a);
            //关闭资源
            fclose($resource);

            //加水印

            $dst_path = "./upload/data/$time1/$imgname.jpg";
            //创建图片的实例
            $dst = imagecreatefromstring(file_get_contents($dst_path));
            //打上文字
            $font = './upload/data/simsun.ttc';//字体
            $black = imagecolorallocate($dst, 255, 255, 255);//字体颜色
            imagefttext($dst, 32, 0, 30, 60, $black, $font, $time);

            header('Content-Type: image/jpeg');
            imagejpeg($dst,"./upload/data/$time1/$imgname.jpg");

            imagedestroy($dst);

            if($customer != 0){
                m('location')->insert("`user`='$this->adminname',`uid`='$this->adminid',`optdt`='$time',`label`='$address',`customerId`='$customer',`type`='1',`explain`='$explain',`imgpath`='http://oa.wxlion.com/upload/data/$time1/$imgname.jpg'");
            }
            else{
                m('location')->insert("`user`='$this->adminname',`uid`='$this->adminid',`optdt`='$time',`label`='$address',`type`='1',`explain`='$explain',`imgpath`='http://oa.wxlion.com/upload/data/$time1/$imgname.jpg'");
            }



        }

        else{

            $this->title = '考勤定位';
            $dt 	= $this->rock->date;
            $dwarr	= m('location')->getrows("uid='$this->adminid' and `optdt` like '$dt%'",'*','`id` desc');

            $this->assign('dwarr', $dwarr);
            $kqrs 	= m('kaoqin')->dwdkrs($this->adminid, $this->date);
            $isgzh	= m('wxgzh:index')->isusegzh();
            $this->assign('isgzh', $isgzh);
            $this->assign('kqrs', $kqrs);
            $dwids	= arrvalue($kqrs, 'dwids');
            $kqors	= array();
            if(!isempt($dwids)){
                $kqors = m('kqdw')->getrows("id in($dwids) and `id`<>".$kqrs['id']."");
            }

            $customer	= m('customer')->getrows("(uid='$this->adminid' or FIND_IN_SET('$this->adminid',shateid)) and status = '1' ",'id,name,status','`id` desc');

//            print_r($customer); die;

            $this->assign('kqors', $kqors);

            $this->assign('customer', $customer);
        }

    }

    public function detailAction(){



        if(isajax()){

            $page = $_POST["page"];
            $uid = $_POST["uid"];

            $pagesize = 5;

            $limit= $page*$pagesize.','.$pagesize;

            $lowArr = m('location')->getall("uid='$uid' and optdt > '2017-08-01' and type ='1'",'optdt,user,id,`explain`,label,imgpath','id desc',$limit);

            $this->returnjson($lowArr);
        }


    }

    public function orderAction(){

//        header('Content-Type: application/json; charset=utf-8');
//
//
//        //请求url地址
//        $appId = 'wxc2a0cb6fd674969a';
//        $appSecret = 'af42fc23fa7be3f61270b7fee216cd45';
//        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
//        //初始化curl
//        $ch = curl_init($url);
//        //3.设置参数
//        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//        //4.调用接口
//        $res = curl_exec($ch);
//        if(curl_errno($ch)){
//            var_dump(curl_error($ch));
//        }
//        $resArr = json_decode($res,1);
//        //5.关闭curl
//        curl_close($ch);
//
//        $access_token = $resArr['access_token'];
//
//
//        $Url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;   //模板信息请求地址
//
//        $data = array(
//
//                 'first'=>array('value'=>"您好,有新的工单",'color'=>"#743A3A"),
//                 'keyword1'=>array('value'=>"狮子王公司",'color'=>'#FF0000'),
//                 'keyword2'=>array('value'=>"9-15 10:30",'color'=>'#FF0000'),
//                 'keyword3'=>array('value'=>"滨湖区科教园",'color'=>'#FF0000'),
//                 'keyword4'=>array('value'=>"电脑维修",'color'=>'#FF0000'),
//                 'keyword5'=>array('value'=>"麻烦准时",'color'=>'#FF0000'),
//                 'remark'=>array('value'=>'谢谢','color'=>'#FF0000'),
//        );
//
//        $template = array(
//            'touser' => 'omgGn0hHKGo5CAK4hjeM_HmCM07g',
//            'template_id' => 'EWTLiCj9_4JHTt9pjxjalnOCrFYvTC2nmcWdISAweNo',
//            'url' => 'http://oa.wxlion.com/?d=we&m=wq&a=order',
//            'topcolor' => 'red',
//            'data' => $data
//        );
//
//        $post_data = json_encode($template);
//        //发送数据，post方式<br>　　　　　　　　　//配置curl请求
//        $ch = curl_init();//创建curl请求
//        curl_setopt($ch, CURLOPT_URL,$Url); //设置发送数据的网址
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设置有返回值，0，直接显示
//        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0); //禁用证书验证
//        curl_setopt($ch, CURLOPT_POST, 1);//post方法请求
//        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//post请求发送的数据包
//        //接收执行返回的数据
//        $data = curl_exec($ch);
//        //关闭句柄
//        curl_close($ch);
//        $data = json_decode($data,true); //将json数据转成数组


    }

    public function wqcxAction()
    {

        m('wxgzh:oauth')->oauthback();


        if(isajax()){

            $page = $_POST["page"];

            $pagesize = 5;

            $limit= $page*$pagesize.','.$pagesize;

            $wqArr = m('location')->getall("uid='$this->adminid' and optdt > '2017-08-01' and type ='1'",'optdt,user,id,`explain`,label,imgpath','id desc',$limit);

            $this->returnjson($wqArr);
        }

        $lowidArr = m('admin')->getall("superid LIKE '%$this->adminid%'",'*','');

        $this->assign('lowidArr', $lowidArr);

    }


    public function lowerwqAction()
    {


        $lowidArr = m('admin')->getall("superid LIKE '%$this->adminid%'",'*','');

//      $this->assign('wqArr', $wqArr);
        $this->assign('lowidArr', $lowidArr);

    }

    public function contentAction()
    {


        if ( isajax() ) {

            $id = $_POST["id"];

            $wqArr =  m('location')->getall(" optdt > '2017-08-01' and id = '$id' and type ='1'",'optdt,user,uid,`explain`,id,label,imgpath','id desc');
            $this->returnjson($wqArr);

        }
        else{
            $isgzh	= m('wxgzh:index')->isusegzh();
            $this->assign('isgzh', $isgzh);
        }

    }

    public function crmAction()
    {



    }


}